﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

namespace CardInChina.Common.Tool
{
    public class CaptchaHelper
    {
        public static string GetRandomNumString(int count)
        {
            int seed = (int)DateTime.Now.Ticks;
            Random rnd = new Random(seed);
            string ret = string.Empty;
            for (int i = 0; i < count; i++)
            {
                ret += rnd.Next(0, 9).ToString();
            }
            return ret;
        }

        public static byte[] CreateCaptchaImagePNG(string CaptchaCode)
        {
            int seed = (int)DateTime.Now.Ticks;
            Random rnd = new Random(seed);
            int rotate = rnd.Next(-5, 5);
            Bitmap objBitmap = null;
            Graphics g = null;
            objBitmap = new Bitmap(18 * CaptchaCode.Length, 30);
            g = Graphics.FromImage(objBitmap);
            g.Clear(Color.White);
            g.TranslateTransform(0, 0);
            g.RotateTransform(rotate);
            g.DrawString(CaptchaCode, new Font("Verdana", 15, FontStyle.Bold), Brushes.OliveDrab, 0, 0);
            //保存图片数据
            MemoryStream stream = new MemoryStream();
            objBitmap.Save(stream, ImageFormat.Png);
            g.Dispose();
            objBitmap.Dispose();
            //输出图片流
            return stream.ToArray();
        }
    }
}
